package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.OrderDao;
import com.aaa.entity.Order;
import com.aaa.entity.OrderInfo;
import com.aaa.entity.YearsAndMonthsCountForOrder;

import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {

    private BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int updateOrderByOrderId(Order order) {
        String sql = "UPDATE `order` SET `cardId` = ?,cardType = ?,`price` = ?,`pay` = ?,`credit` = ?,`status` = ?,`momo` = ? WHERE `orderId` = ?";
        Object[] paramsArr = {order.getCardId(), order.getCardType(), order.getPrice(), order.getPay(), order.getCredit(), order.getStatus(), order.getMomo(), order.getOrderId()};
        return baseDao.executeUpdate(sql, paramsArr);
    }

    @Override
    public int updateVerifyOrderByOrderId(Integer orderId, Integer status) {
        int statusForUpdate=0;
        if(status==0){
            statusForUpdate=1;
        }
        String sql = "UPDATE `order` SET `status` = ? WHERE `orderId` = ?";
        Object[] paramsArr = {statusForUpdate, orderId};
        return baseDao.executeUpdate(sql, paramsArr);
    }

	/**
     * 查询所有订单
     * @param pageNumber
     * @param pageSize
     * @param orderId
     * @param cardId
     * @return
     */
    @Override
    public List<Order> findAllOrder(Integer pageNumber, Integer pageSize, Integer orderId, Integer cardId) {
        pageNumber = (pageNumber - 1) * pageSize;
        String sql = "select * from `order` where 1=1";
        if (orderId > 0) {
            sql += " and orderId = " + orderId;
        }
        if (cardId > 0) {
            sql += " and cardId like '" + "%" + cardId + "%" + "'";
        }
        sql += " limit ?,?";
        System.out.println(sql);
        System.out.println(pageNumber);
        Object[] params = {pageNumber, pageSize};
        List<Order> orderList = baseDao.query(sql, params, Order.class);
        return orderList;
    }

    @Override
    public List<Order> findOrderByOrderId(int orderId) {
        return null;
    }

    @Override
    public List<Order> findOrderByCardId(int cardId) {
        return null;
    }

    /**
     * 统计所有的订单总数（包括查询）
     * @param orderId
     * @param cardId
     * @return
     */
    @Override
    public int getAllOrderInfoCount(Integer orderId, Integer cardId) {
        String sql = "select count(1) len from `order` where 1 = 1";
        if (orderId > 0) {
            sql += " and staffId = " + orderId;
        }
        if (cardId > 0) {
            sql += " and staffName like '" + "%" + cardId + "%" + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        } else {
            return 0;

        }
    }

	/**
     * 充值统计
     * @return
     */
    @Override
    public List<YearsAndMonthsCountForOrder> getOrderNearYearMonthsCount(){
        Object[] arr = {};
        String sql="select a.`months`,b.`values` from(SELECT DATE_FORMAT(curdate(),'%Y-%m') `months` " +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 1 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 2 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 4 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 5 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 7 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 8 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 9 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 10 month),'%Y-%m') `months`" +
                "                 union all SELECT DATE_FORMAT(date_sub(curdate(), interval 11 month),'%Y-%m') `months`" +
                "                )a left join (select DATE_FORMAT(createdTime,'%Y-%m') `months`,count(*) `values` from `order` " +
                "                where createdTime between date_add(now(),interval -1 year) and now() group by `months`" +
                "                ) b on a.`months` = b.`months` group by a.`months` order by a.`months`";

        List<YearsAndMonthsCountForOrder> yearsAndMonthsCountForOrderList =baseDao.query(sql, arr, YearsAndMonthsCountForOrder.class);
        return yearsAndMonthsCountForOrderList;
    }
	
	/**
     * 提交订单更新order表
     *
     * @param order
     * @return
     */
    @Override
    public int insertIntoOrder(Order order) {
        String sql = "INSERT INTO `order` (`orderId`, `cardId`, `cardType`, `price`, `pay`, `credit`, `status`, `momo`,`createdTime`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        //积分没用到，默认为1
        Object[] arr = {order.getOrderId(), order.getCardId(), order.getCardType(), order.getPrice(), order.getPay(),
                order.getCredit(), order.getStatus(), order.getMomo(), order.getCreatedTime()};
        return baseDao.executeUpdate(sql, arr);
    }

    /**
     * 提交订单更新orderinfo表
     *
     * @param orderInfo
     * @return
     */
    @Override
    public int insertIntoOrderinfo(OrderInfo orderInfo) {
        String sql = "INSERT INTO `orderinfo` (`orderId`, `goodsId`, `goodsNumber`, `goodsCredit`) VALUES (?,?,?,?)";
        //积分没用到，默认为1
        Object[] arr = {orderInfo.getOrderId(), orderInfo.getGoodsId(), orderInfo.getGoodsNumber(), 1};
        return baseDao.executeUpdate(sql, arr);
    }


    /**
     * 减去用户提交订单后的余额
     * @param cardId
     * @param money
     * @return
     */
    @Override
    public int minusCardAmount(int cardId, double money) {
        String sql = "UPDATE `card` SET `amount`=`amount`-? WHERE `cardId`=?";
        //积分没用到，默认为1
        Object[] arr = {money,cardId};
        return baseDao.executeUpdate(sql, arr);
    }


    @Override
    public int addOrder(Order order) {
        String sql="INSERT INTO `orderinfo` (`orderId`, `goodsId`, `goodsNumber`, `goodsCredit`) VALUES (?,?,?,?)";
        Object[] arr = {order};
        return baseDao.executeUpdate(sql,arr);

    }

}
